package com.renge.cmsservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.renge.cmsservice.entity.CrmBanner;
import com.renge.cmsservice.entity.json.BannerQueryJson;
import com.renge.cmsservice.service.CrmBannerService;
import com.renge.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-15
 */
@Api(description="首页轮播图后台管理")
@RestController
@CrossOrigin
@RequestMapping("/cmsservice/crm-banner")
public class CrmBannerController {
    @Autowired
    private CrmBannerService crmBannerService;

    @ApiOperation(value = "根据条件分页查询")
    @PostMapping("page/{current}/{pagesize}")
    public R getByWrapperPage(@ApiParam(name = "current",value = "当前页",required = true)
                              @PathVariable("current")Integer current,
                              @ApiParam(name = "pagesize",value = "每页数据量",required = true)
                              @PathVariable("pagesize")Integer pagesize,
                              @ApiParam(name = "bannerQueryJson", value = "查询对象", required = false)
                              @RequestBody BannerQueryJson bannerQueryJson){
        IPage<CrmBanner> page=crmBannerService.getByWrapperPage(current,pagesize,bannerQueryJson);
        //组装data
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",page.getTotal());
        hashMap.put("rows",page.getRecords());
        return R.ok().data(hashMap);
    }
    @ApiOperation(value = "查询单个crmBanner")
    @GetMapping("{crmBannerId}")
    public R getByWrapperCrmBanner(@ApiParam(name = "crmBannerId",value = "轮播图id",required = true)
                                   @PathVariable("crmBannerId")String crmBannerId){
        CrmBanner crmBanner = crmBannerService.getById(crmBannerId);
        //判断
        if(StringUtils.isEmpty(crmBanner)){
            return R.error();
        }
        return R.ok().data("crmBanner",crmBanner);
    }
    @ApiOperation(value = "保存crmBanner到数据库")
    @CacheEvict(value = "banner", key = "'selectIndexList'", allEntries=true)
    @PostMapping("save")
    public R saveCrmBanner(@ApiParam(name = "crmBanner",value = "封装轮播图数据",required = true)
                                   @RequestBody CrmBanner crmBanner ){
        boolean save = crmBannerService.save(crmBanner);
        //判断
        if(!save){
            return R.error();
        }
        return R.ok();
    }
    @ApiOperation(value = "更新crmBanner到数据库")
    @PutMapping("update")
    @CacheEvict(value = "banner", key = "'selectIndexList'", allEntries=true)
    public R updateCrmBanner(@ApiParam(name = "crmBanner",value = "封装轮播图数据",required = true)
                           @RequestBody CrmBanner crmBanner ){
        boolean update = crmBannerService.updateById(crmBanner);
        //判断
        if(!update){
            return R.error();
        }
        return R.ok();
    }
    @ApiOperation(value = "删除crmBanner到数据库")
    @DeleteMapping("{crmBannerId}")
    public R deleteCrmBannerById(@ApiParam(name = "crmBannerId",value = "轮播图的id",required = true)
                                     @PathVariable("crmBannerId")String crmBannerId ){
        crmBannerService.deleteCrmBannerById(crmBannerId);
        return R.ok();
    }

}

